package com.glowdayz.glowmee;

import com.glowdayz.common.ActivityManager;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BrandEvent extends FragmentActivity implements OnClickListener {
	
	private ActivityManager am = ActivityManager.getInstance();
	
	int paramId = 0;
	
	int fragmentIndex = 0;
    public final static int FRAGMENT_ONE = 1;
    public final static int FRAGMENT_TWO = 2;
	
    ImageView nowEvent = null, beforeEvent = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_brand_event);
		
		am.addActivity(this);
		
		ImageView backBt = (ImageView) findViewById(R.id.backBt);
		backBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
		backBt.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		ImageView okBt = (ImageView) findViewById(R.id.okBt);
		okBt.setVisibility(View.GONE);
		
		Intent fromIntent = getIntent();
		paramId = fromIntent.getIntExtra("PARAM_paramId", 0);
		String paramTitle = fromIntent.getStringExtra("PARAM_paramTitle");
		
		TextView textTitle = (TextView) findViewById(R.id.textTitle);
		textTitle.setText(paramTitle);
		
		nowEvent = (ImageView) findViewById(R.id.nowEvent);
		beforeEvent = (ImageView) findViewById(R.id.beforeEvent);
		nowEvent.setOnClickListener(this);
		beforeEvent.setOnClickListener(this);
		
		fragmentIndex = FRAGMENT_ONE;
		fragmentReplace(fragmentIndex);
	}
	
	public void fragmentReplace(int reqNewFragmentIndex) {
		Bundle bundle = new Bundle();
		bundle.putInt("PARAM_event", fragmentIndex);
		bundle.putInt("PARAM_idBrand", paramId);
        Fragment newFragment = new EventFm();
        newFragment.setArguments(bundle);
        final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.ll_fragment, newFragment);
        transaction.commit();
    }
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.nowEvent:
			nowEvent.setImageResource(R.drawable.tap_on_ing);
			beforeEvent.setImageResource(R.drawable.tap_off_ed);
			fragmentIndex = FRAGMENT_ONE;
            fragmentReplace(fragmentIndex);
			break;
		case R.id.beforeEvent:
			nowEvent.setImageResource(R.drawable.tap_off_ing);
			beforeEvent.setImageResource(R.drawable.tap_on_ed);
			fragmentIndex = FRAGMENT_TWO;
            fragmentReplace(fragmentIndex);
			break;
		}
	}
	
    @Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}

}
